package com.bigflexlib.x.mx.collections.IList.op
{
	import com.bigflexlib.core.markers.IIncomplete;
	import com.bigflexlib.x.flash.display.DisplayObjectContainerUtil;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import mx.collections.IList;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;




	/**
	 * 
	 * TODO: keep order
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class ListToDisplayObjectContainerChildrenOp extends AbstractListOp implements IIncomplete
	{
		
		private var oDoc:DisplayObjectContainer;
		
		
		public function ListToDisplayObjectContainerChildrenOp( iList:IList, oDoc:DisplayObjectContainer )
		{
			super( iList );
			this.oDoc = oDoc;
		}
		
		
		
		
		
		override protected function iList_collectionChange( event:CollectionEvent ):void
		{
			
			var dobj:DisplayObject;
			
			switch ( event.kind )
			{
			
				case CollectionEventKind.ADD :
					
					for each ( dobj in event.items )
						oDoc.addChild( dobj );
									
				break;
				
				
				case CollectionEventKind.REMOVE :
				
					for each ( dobj in event.items )
						if ( oDoc.contains( dobj ) )
							oDoc.removeChild( dobj );
				
				break;
			
				case CollectionEventKind.UPDATE :
				break;
				
				default:
					refresh( );
				break;
				
			}
				
		
		}		
		
		
		
		private function refresh( ):void
		{
			DisplayObjectContainerUtil.removeAllChildren( oDoc );
			for each ( var dobj:DisplayObject in iList.toArray() )
			{
				oDoc.addChild( dobj );
			}
		}
		
		
		
		
		override public function dispose( ):void
		{
			super.dispose( );
			oDoc = null;
		}		
		
	}
}